//
// ElfParser.h
// Usage: Parse ELF module file
// CreateDate: 2009-04-23
// Author: MaJiuyue
//
#ifndef _ELFPARSER_H_
#define _ELFPARSER_H_

#include <type.h>

struct Elf32Header
{
	u8 e_ident[16];
	u16 e_type;
	u16 e_machine;
	u32 e_version;
	u32 e_entry;
	u32 e_phoff;
	u32 e_shoff;
	u32 e_flags;
	u16 e_ehsize;
	u16 e_phentsize;
	u16 e_phnum;
	u16 e_shentsize;
	u16 e_shnum;
	u16 e_shstrndx;
};

struct Elf32ProgramHeader
{
	u32 ph_type;
	u32 ph_offset;
	u32 ph_vaddr;
	u32 ph_paddr;
	u32 ph_filesz;
	u32 ph_memsz;
	u32 ph_flags;
	u32 ph_align;
};

struct Elf32SectionHeader
{
	u32 sh_name;
	u32 sh_type;
	u32 sh_flags;
	u32 sh_addr;
	u32 sh_offset;
	u32 sh_size;
	u32 sh_link;
	u32 sh_info;
	u32 sh_addralign;
	u32 sh_entsize;
};

class ElfParser
{
private:
	Elf32Header * m_ElfHeader;
	int m_index;

public:
	/* Construct a ElfParser from elf buffer */
	ElfParser(void * elf_buffer);

	/* Get ELF's entry point address */
	addr_t GetEntryAddr();

	/* Section operations */
	void SegmentFirst();
	bool SegmentNext();
	addr_t GetOffset();
	addr_t GetMemSize();
	addr_t GetFileSize();
	addr_t GetVaddr();
};

#endif

